package cn.nb12.okhttp3.service.impl;

import java.io.IOException;

import org.springframework.stereotype.Service;

import cn.nb12.okhttp3.po.Root;
import cn.nb12.okhttp3.service.IWeather;
import cn.nb12.okhttp3.service.RetrofitWeatherService;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

@Service
public class RetrofitWeatherServiceImpl implements RetrofitWeatherService {

	@Override
	public Root findWeatherByCode(String code) {
		Retrofit retrofit = new Retrofit.Builder().baseUrl("http://www.weather.com.cn/data/cityinfo/")
				.addConverterFactory(GsonConverterFactory.create()).build();

		IWeather service = retrofit.create(IWeather.class);
		Call<Root> repos = service.getWeather(code);
		try {
			Response<Root> execute = repos.execute();
			return execute.body();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

}
